﻿Imports System.Diagnostics.CodeAnalysis

#Region "MRUFileInfo"

''' <summary>
''' 
''' </summary>
    <Flags()> _
    <SuppressMessage("Microsoft.Naming", "CA1714:FlagsEnumsShouldHavePluralNames")>
Public Enum MRUFileInfo
    ''' <summary>
    ''' 
    ''' </summary>
        Path = 1
    ''' <summary>
    ''' 
    ''' </summary>
        FileName = 2
    ''' <summary>
    ''' 
    ''' </summary>
        Extension = 4
    ''' <summary>
    ''' 
    ''' </summary>
        PathFileName = 3
    ''' <summary>
    ''' 
    ''' </summary>
        FileNameExtension = 6
End Enum

#End Region

#Region "MRUFileSortTypes"

''' <summary>Enumeration of file sort types.</summary>
    <SuppressMessage("Microsoft.Naming", "CA1717:OnlyFlagsEnumsShouldHavePluralNames")>
Public Enum MRUFileSortTypes
    ''' <summary>
    ''' 
    ''' </summary>
        AscendingModifiedDate = 1
    ''' <summary>
    ''' 
    ''' </summary>
        DescendingModifiedDate = 2
    ''' <summary>
    ''' 
    ''' </summary>
        AscendingName = 3
    ''' <summary>
    ''' 
    ''' </summary>
        DescendingName = 4
    ''' <summary>
    ''' 
    ''' </summary>
        AscendingSize = 5
    ''' <summary>
    ''' 
    ''' </summary>
        DescendingSize = 6
End Enum

#End Region

#Region "MRUFiles"

''' <summary>common code to work with the file system.  This class provides extra functions over the .NET 
''' File, Path, and Directory class.</summary>
    <DebuggerStepThrough()>
Public NotInheritable Class MRUFiles
    Private Sub New()
    End Sub

    ''' <summary>Gets Parts of a file name.  You can get the following parts:  Path -- no trailing backslash, 
    ''' FileName -- Data between last backslash and last period, and extension -- data from the last period 
    ''' on.</summary>
    ''' <param name="szFile">Path and file name to process</param>
    ''' <param name="eType">Bit field enum that specifies what you want</param>
    ''' <returns>The requested file parts</returns>
    ''' <example>
    ''' GetFileInfo("C:\Temp\test\lone.star.1", MRUFileInfo.Path) == "C:\Temp\test"
    ''' GetFileInfo("C:\Temp\test\lone.star.1", MRUFileInfo.Filename) == "lone.star"
    ''' GetFileInfo("C:\Temp\test\lone.star.1", MRUFileInfo.Extension) == ".1"
    ''' GetFileInfo("C:\Temp\test\lone.star.1", MRUFileInfo.FileNameExtension) == "lone.star.1"
    ''' GetFileInfo("C:\Temp\test\lone.star.1", MRUFileInfo.PathFileName) == "C:\Temp\test\lone.star"
    ''' </example>
    Public Shared Function GetFileInfo(szFile As String, eType As MRUFileInfo) As String
        Dim szTemp As String = String.Empty
        Dim bPath As Boolean = False

        If (eType And MRUFileInfo.Path) <> 0 Then
            szTemp = Path.GetDirectoryName(szFile)
            bPath = True
        End If

        If (eType And MRUFileInfo.FileName) <> 0 Then
            If bPath Then
                szTemp += "\"c
            End If
            szTemp = szTemp + Path.GetFileNameWithoutExtension(szFile)
        End If

        If (eType And MRUFileInfo.Extension) <> 0 Then
            szTemp = szTemp + Path.GetExtension(szFile)
        End If

        Return szTemp
    End Function

    Public Shared Sub DeleteOld(ByVal directoryPath As String, ByVal searchPattern As String, ByVal daysOld As Integer, ByVal recursive As Boolean, ByVal deleteSubDirs As Boolean)
        Dim aszFiles As String() = Directory.GetFiles(directoryPath, searchPattern)
        For Each szFile As String In aszFiles
            Dim dtFile As DateTime = File.GetCreationTime(szFile)
            Dim oDif As TimeSpan = DateTime.Now - dtFile
            If oDif.Days > daysOld Then
                Helper.DeleteNoError(szFile)
            End If
        Next
        If recursive Then
            Dim aszSubDirs As String() = Directory.GetDirectories(directoryPath)
            For Each szSubDir As String In aszSubDirs
                DeleteOld(szSubDir, searchPattern, daysOld, recursive, deleteSubDirs)
                If deleteSubDirs Then
                    Try
                        Directory.Delete(szSubDir)
                    Catch
                        'no error if can't delete.
                    End Try
                End If
            Next
        End If
    End Sub
End Class

#End Region